package com.frinder.robot.handler;

import com.frinder.base.akka.AbstractRequest;
import com.frinder.robot.strategy.action.ConvertTaskToActionHandler;
import com.frinder.robot.strategy.action.ConvertTaskToActionPayload;
import com.frinder.robot.task.BaseRobotTask;
import lombok.Builder;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @ClassName RobotActionActor
 * @Description 机器人动作转化类
 * @Author frinder
 * @Date 2019/10/22 10:36
 * @Verion 1.0
 */
@Component
@Scope("prototype")
public class RobotActionActor extends BaseRobotActor {

    public static final String NAME = "robot-action-actor-%s";

    @Autowired
    private ConvertTaskToActionHandler convertTaskToActionHandler;

    public RobotActionActor(String robotName) {
        super(robotName);
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(ApplyForTaskToActionPayload.class, this::applyForTaskToAction)
                .build();
    }

    /**
     * 转化任务为动作
     *
     * @param payload
     */
    private void applyForTaskToAction(ApplyForTaskToActionPayload payload) {
        convertTaskToActionHandler.handle(ConvertTaskToActionPayload.builder()
                .sender(getSender())
                .receiver(getSelf())
                .task(payload.task)
                .build()
        );
    }

    /**
     * 转化动作响应
     */
    @ToString(callSuper = true)
    public static class RespondActionForTaskPayload extends AbstractRequest {
        @Builder
        private RespondActionForTaskPayload(String traceId) {
            super(traceId);
        }
    }

    /**
     * 申请转化任务为动作请求
     */
    @ToString(callSuper = true)
    public static class ApplyForTaskToActionPayload extends AbstractRequest {

        private BaseRobotTask task;

        @Builder
        private ApplyForTaskToActionPayload(String traceId, BaseRobotTask task) {
            super(traceId);
            this.task = task;
        }
    }
}
